﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using nERP.Model.Wms;
using nERP.Model;
using nERP.Model.So;
using nERP.Model.Bas;
using nERP.Model.Po;

namespace nERP.ViewModel.Wms.SaleOutWarehouseDetailVMs
{
    public partial class SaleOutWarehouseDetailListVM : BasePagedListVM<SaleOutWarehouseDetail_View, SaleOutWarehouseDetailSearcher>
    {

        protected override IEnumerable<IGridColumn<SaleOutWarehouseDetail_View>> InitGridHeader()
        {
            return new List<GridColumn<SaleOutWarehouseDetail_View>>{

                this.MakeGridHeader(x => x.SaleOutWarehouseDetail_Seq).SetTitle(@Localizer["Page.序号"].Value),
                this.MakeGridHeader(x => x.SaleOutWarehouseDetail_SaleOutWarehouse).SetTitle(@Localizer["Page.销售出库"].Value),
                this.MakeGridHeader(x => x.SaleOutWarehouseDetail_SaleOutWarehouseDetailCode).SetTitle(@Localizer["Page.销售出库明细单号"].Value),
                this.MakeGridHeader(x => x.SaleOutWarehouseDetail_Sale).SetTitle(@Localizer["Page.销售订单"].Value),
                this.MakeGridHeader(x => x.SaleOutWarehouseDetail_SaleDetail).SetTitle(@Localizer["Page.销售明细"].Value),
                this.MakeGridHeader(x => x.SaleOutWarehouseDetail_Goods).SetTitle(@Localizer["Page.商品"].Value),
                this.MakeGridHeader(x => x.SaleOutWarehouseDetail_GoodsName).SetTitle(@Localizer["Page.商品名称"].Value),
                this.MakeGridHeader(x => x.SaleOutWarehouseDetail_GoodsSpec).SetTitle(@Localizer["Page.商品规格"].Value),
                this.MakeGridHeader(x => x.SaleOutWarehouseDetail_GoodsUnit).SetTitle(@Localizer["Page.计量单位"].Value),
                this.MakeGridHeader(x => x.SaleOutWarehouseDetail_Warehouse).SetTitle(@Localizer["Page.仓库"].Value),
                this.MakeGridHeader(x => x.SaleOutWarehouseDetail_SaleOutNumber).SetTitle(@Localizer["Page.出库数量"].Value),
                this.MakeGridHeader(x => x.SaleDetail_UnitPrice).SetTitle(@Localizer["Page.单价"].Value),
                this.MakeGridHeader(x => x.SaleDetail_TotalPrice).SetTitle(@Localizer["Page.金额"].Value),
                this.MakeGridHeader(x => x.SaleDetail_TaxRate).SetTitle(@Localizer["Page.税率"].Value),
                this.MakeGridHeader(x => x.SaleDetail_DiscountPrice).SetTitle(@Localizer["Page.折扣金额"].Value),
                this.MakeGridHeader(x => x.SaleDetail_TaxUnitPrice).SetTitle(@Localizer["Page.含税单价"].Value),
                this.MakeGridHeader(x => x.SaleDetail_TaxPrice).SetTitle(@Localizer["Page.税额"].Value),
                this.MakeGridHeader(x => x.SaleDetail_TotalTaxPrice).SetTitle(@Localizer["Page.含税总价"].Value),
                this.MakeGridHeader(x => x.SaleOutWarehouseDetail_Remark).SetTitle(@Localizer["_Admin.Remark"].Value),
                this.MakeGridHeader(x => x.SaleOutWarehouseDetail_Status).SetTitle(@Localizer["Page.状态"].Value),
                this.MakeGridHeader(x => x.SaleOutWarehouseDetail_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.SaleOutWarehouseDetail_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.SaleOutWarehouseDetail_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.SaleOutWarehouseDetail_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),

                this.MakeGridHeaderAction(width: 200)
            };
        }


        public override IOrderedQueryable<SaleOutWarehouseDetail_View> GetSearchQuery()
        {
            var query = DC.Set<SaleOutWarehouseDetail>()

                .CheckEqual(Searcher.Seq, x => x.Seq)
                .CheckEqual(Searcher.SaleOutWarehouseId, x => x.SaleOutWarehouseId)
                .CheckContain(Searcher.SaleOutWarehouseDetailCode, x => x.SaleOutWarehouseDetailCode)
                .CheckEqual(Searcher.SaleId, x => x.SaleId)
                .CheckEqual(Searcher.SaleDetailId, x => x.SaleDetailId)
                .CheckEqual(Searcher.GoodsId, x => x.GoodsId)
                .CheckContain(Searcher.GoodsName, x => x.GoodsName)
                .CheckContain(Searcher.GoodsSpec, x => x.GoodsSpec)
                .CheckContain(Searcher.GoodsUnit, x => x.GoodsUnit)
                .CheckEqual(Searcher.WarehouseId, x => x.WarehouseId)
                .CheckEqual(Searcher.SaleOutNumber, x => x.SaleOutNumber)
                .CheckContain(Searcher.Remark, x => x.Remark)
                .CheckEqual(Searcher.Status, x => x.Status)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x => x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x => x.UpdateBy)
                .Select(x => new SaleOutWarehouseDetail_View
                {
                    ID = x.ID,

                    SaleOutWarehouseDetail_Seq = x.Seq,
                    SaleOutWarehouseDetail_SaleOutWarehouse = DC.Set<SaleOutWarehouse>().Where(z0 => z0.SaleOutWarehouseCode == x.SaleOutWarehouseId).Select(y => y.SaleOutWarehouseCode).FirstOrDefault(),
                    SaleOutWarehouseDetail_SaleOutWarehouseDetailCode = x.SaleOutWarehouseDetailCode,
                    SaleOutWarehouseDetail_Sale = DC.Set<Sale>().Where(z0 => z0.SaleCode == x.SaleId).Select(y => y.SaleCode).FirstOrDefault(),
                    SaleOutWarehouseDetail_SaleDetail = DC.Set<SaleDetail>().Where(z0 => z0.SaleDetailCode == x.SaleDetailId).Select(y => y.SaleDetailCode).FirstOrDefault(),
                    SaleOutWarehouseDetail_Goods = x.GoodsId,// DC.Set<Goods>().Where(z0 => z0.GoodsCode == x.GoodsId).Select(y => y.GoodsName).FirstOrDefault(),
                    SaleOutWarehouseDetail_GoodsName = x.GoodsName,
                    SaleOutWarehouseDetail_GoodsSpec = x.GoodsSpec,
                    SaleOutWarehouseDetail_GoodsUnit = x.GoodsUnit,
                    SaleOutWarehouseDetail_Warehouse = DC.Set<Warehouse>().Where(z0 => z0.WarehouseCode == x.WarehouseId).Select(y => y.WarehouseName).FirstOrDefault(),
                    SaleOutWarehouseDetail_SaleOutNumber = x.SaleOutNumber,

                    SaleDetail_UnitPrice = DC.Set<SaleDetail>().Where(z0 => z0.SaleDetailCode == x.SaleDetailId).Select(y => y.UnitPrice).FirstOrDefault(),
                    SaleDetail_DiscountPrice = DC.Set<SaleDetail>().Where(z0 => z0.SaleDetailCode == x.SaleDetailId).Select(y => y.DiscountPrice).FirstOrDefault(),
                    SaleDetail_TotalPrice = DC.Set<SaleDetail>().Where(z0 => z0.SaleDetailCode == x.SaleDetailId).Select(y => y.TotalPrice).FirstOrDefault(),
                    SaleDetail_TaxRate = DC.Set<SaleDetail>().Where(z0 => z0.SaleDetailCode == x.SaleDetailId).Select(y => y.TaxRate).FirstOrDefault(),
                    SaleDetail_TaxUnitPrice = DC.Set<SaleDetail>().Where(z0 => z0.SaleDetailCode == x.SaleDetailId).Select(y => y.TaxUnitPrice).FirstOrDefault(),
                    SaleDetail_TaxPrice = DC.Set<SaleDetail>().Where(z0 => z0.SaleDetailCode == x.SaleDetailId).Select(y => y.TaxPrice).FirstOrDefault(),
                    SaleDetail_TotalTaxPrice = DC.Set<SaleDetail>().Where(z0 => z0.SaleDetailCode == x.SaleDetailId).Select(y => y.TotalTaxPrice).FirstOrDefault(),

                    SaleOutWarehouseDetail_Remark = x.Remark,
                    SaleOutWarehouseDetail_Status = x.Status,
                    SaleOutWarehouseDetail_CreateTime = x.CreateTime,
                    SaleOutWarehouseDetail_UpdateTime = x.UpdateTime,
                    SaleOutWarehouseDetail_CreateBy = x.CreateBy,
                    SaleOutWarehouseDetail_UpdateBy = x.UpdateBy,
                })
                .OrderBy(x => x.SaleOutWarehouseDetail_Seq);
            return query;
        }


    }
    public class SaleOutWarehouseDetail_View : SaleOutWarehouseDetail
    {

        public int? SaleOutWarehouseDetail_Seq { get; set; }
        public string SaleOutWarehouseDetail_SaleOutWarehouse { get; set; }
        public string SaleOutWarehouseDetail_SaleOutWarehouseDetailCode { get; set; }
        public string SaleOutWarehouseDetail_Sale { get; set; }
        public string SaleOutWarehouseDetail_SaleDetail { get; set; }
        public string SaleOutWarehouseDetail_Goods { get; set; }
        public string SaleOutWarehouseDetail_GoodsName { get; set; }
        public string SaleOutWarehouseDetail_GoodsSpec { get; set; }
        public string SaleOutWarehouseDetail_GoodsUnit { get; set; }
        public string SaleOutWarehouseDetail_Warehouse { get; set; }
        public decimal? SaleOutWarehouseDetail_SaleOutNumber { get; set; }
        public decimal? SaleDetail_UnitPrice { get; set; }
        public decimal? SaleDetail_TotalPrice { get; set; }
        public decimal? SaleDetail_TaxRate { get; set; }
        public decimal? SaleDetail_DiscountPrice { get; set; }
        public decimal? SaleDetail_TaxUnitPrice { get; set; }
        public decimal? SaleDetail_TaxPrice { get; set; }
        public decimal? SaleDetail_TotalTaxPrice { get; set; }
        public string SaleOutWarehouseDetail_Remark { get; set; }
        public dStatusTypeEnum? SaleOutWarehouseDetail_Status { get; set; }
        public DateTime? SaleOutWarehouseDetail_CreateTime { get; set; }
        public DateTime? SaleOutWarehouseDetail_UpdateTime { get; set; }
        public string SaleOutWarehouseDetail_CreateBy { get; set; }
        public string SaleOutWarehouseDetail_UpdateBy { get; set; }

    }

}